﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

///<summary>
///
///</summary>
public class ImageSpriteChageComponet : MonoBehaviour
{
    [SerializeField]
    private Image image;
    public List<Sprite> ani1 = new List<Sprite>();
    public float speed;
    public float time;
    public bool aniFinish = false;
    public void OnEnable()
    {
        PlayAnimation();
        aniFinish = false;
    }

    public void PlayAnimation()
    {
        if (image == null) image = GetComponent<Image>();
        if (time == 1)
            StartCoroutine(PlayAnimationOnce());
        else if (time == -1)
            StartCoroutine(PlayAnimationForwardIEnum());
        else
            Debug.LogError("Dont time");
    }
    private IEnumerator PlayAnimationForwardIEnum()
    {
        int index = 0;//能够用来控制起始播放的动画帧索引
        gameObject.SetActive(true);
        while (true)
        {
            //当咱们须要在整个动画播放完以后  重复播放后面的部分 就能够展示咱们纯代码播放的自由性
            if (index < ani1.Count-1)
            {
                image.sprite = ani1[index];
                index++;
            }
            else
            {
                index = 0;
            }
            yield return new WaitForSeconds(speed);//等待间隔  控制动画播放速度
        }
    }
    public IEnumerator PlayAnimationOnce()
    {
        int index = 0;//可以用来控制起始播放的动画帧索引
        gameObject.SetActive(true);
        while (true)
        {
            //当我们需要在整个动画播放完之后 重复播放后面的部分 就可以展现我们纯代码播放的自由性
            if (index < ani1.Count)
            {
                image.sprite = ani1[index];
                index++;
            }
            else
            {
                aniFinish = true;
            }
            yield return new WaitForSeconds(speed);//等待间隔 控制动画播放速度
        }
    }
}
